<?php
class GetPager extends Controller
{
	// Global search value array
	private $arrSearchValues = array();
	
	public function __construct()
	{
		parent::__construct();		
	}
	
	public function run()
	{
		$str  = $this->generatePager();
		echo $str;
	}
	
	public function generatePager()
	{
		$this->import('Database');
		$search_action	 = $_GET['form_filter'];
		if(isset($search_action) && $search_action=="action"){
			$condition   = $this->postCondition();
			$sql		= "SELECT `product_catalog`.`id` 
							FROM `product_catalog` 
							LEFT JOIN `catalog_tags` ON `product_catalog`.`tags` LIKE CONCAT('%,', `catalog_tags`.`id`, '') OR `product_catalog`.`tags` LIKE CONCAT('', `catalog_tags`.`id`, ',%') OR `product_catalog`.`tags` LIKE CONCAT('%,', `catalog_tags`.`id`, ',%') OR `product_catalog`.`tags` LIKE `catalog_tags`.`id`
							LEFT JOIN `tl_catalog_attribute` ON `tl_catalog_attribute`.`id_catalog`=product_catalog.`id`							
                            JOIN `catalog_colour` ON `catalog_colour`.`id`=`tl_catalog_attribute`.`id_color`
							JOIN `catalog_size` ON `catalog_size`.`id`=`tl_catalog_attribute`.`id_size` 
							$condition 
							GROUP BY `tl_catalog_attribute`.`id_catalog`";				
		}else{
			$condition   = $this->getCondition();
			if(isset($_GET['tag'])){
				$sql = "SELECT `product_catalog`.`id` FROM `product_catalog` JOIN `catalog_tags` ON `product_catalog`.`tags` LIKE CONCAT('%,', `catalog_tags`.`id`, '') OR `product_catalog`.`tags` LIKE CONCAT('', `catalog_tags`.`id`, ',%') OR `product_catalog`.`tags` LIKE CONCAT('%,', `catalog_tags`.`id`, ',%') OR `product_catalog`.`tags` LIKE `catalog_tags`.`id` 
$condition 
GROUP BY `product_catalog`.`id`";
			}else{
				$sql = "SELECT `product_catalog`.`id` FROM `product_catalog` $condition";
			}
		}
		
		$countRd 	= $this->Database->prepare($sql)
									->execute($this->arrSearchValues, time(), time()); 
		if ($countRd->numRows > 0)
		{
			//$countRd->next();
			$rsNum		= ceil($countRd->numRows/$_SESSION['perPageRecord']);
			$arrNum = array(
				'pageTotalNum' => $rsNum,
				'total' => $countRd->numRows,
			);
			$jsonNum = json_encode($arrNum);
			echo $jsonNum;
		}
		else
		{
			echo 0;
		}
	}	
	
	public function postCondition()
	{
		if($_SESSION['perPageRecord'] == ""){
			$_SESSION['perPageRecord'] = 8;
		}else{
			if(isset($_GET['perPageRecord']) && $_GET['perPageRecord'] != "") $_SESSION['perPageRecord'] = $_GET['perPageRecord'];
		}	
	
		$search_keyword  = ucfirst(trim($_GET['product_keyword']));
		$search_category = $_GET['ads_category'];
		$search_brand	 = $_GET['ads_brand'];
		$search_colour	 = $_GET['ads_colour'];
		$search_size	 = $_GET['ads_size'];
		$search_price	 = $_GET['ads_price'];
		
		
		$condition = " where published=1 ";
		
		if ($search_keyword == "" || $search_keyword == "SEARCH A PRODUCT")
		{
			
		}
		else
		{		
			// Product Keyword
			if (isset($search_keyword) && $search_keyword)
			{
				preg_match_all('/"[^"]+"|[\+\-]?[^ ]+\*?/', $search_keyword, $arrChunks);
			}
			
			if (isset($arrChunks[0]) && is_array($arrChunks[0]) && $arrChunks[0])
			{
				$condition .= " And (";
				$index = 0;
				foreach ($arrChunks[0] as $strChunks)
				{
					$condition .= (($index > 0) ? " OR " : "") . "`name` LIKE ? 
						OR  `catalog_colour`.`title` LIKE ?  OR `catalog_tags`.`tag` LIKE ?";
						
					$this->arrSearchValues[] = "%" . $strChunks . "%";
					$this->arrSearchValues[] = "%" . $strChunks . "%";
					$this->arrSearchValues[] = "%" . $strChunks . "%";
					//$this->arrSearchValues[] = "%" . $strChunks . "%";
					
					$index++;
				}
				$condition .= ") ";
			}
		}
		
		/*if($search_keyword != ""){
			$condition .= " And (
				`name` LIKE '%".$search_keyword."%' 
				OR `description` LIKE '%".$search_keyword."%' OR  `catalog_colour`.`title` LIKE '%".$search_keyword."%'  OR `catalog_tags`.`tag` LIKE '%".$search_keyword."%') ";
		}*/

		// Category Or Subcategory
		if($search_category != "all"){
			$sql 	 = "SELECT pid FROM tl_taxonomy where id=".$search_category; 
			$countRd = $this->Database->execute($sql); 
			$countRd->next();

			if($countRd->pid==0){
				$condition .= " And category IN ( SELECT id FROM `tl_taxonomy` WHERE pid =".$search_category.") ";
			}else{
				$condition .= " And category=".$search_category;
			}
		}		

		// Brand
		if($search_brand != "all") $condition .= " And brand=".$search_brand;

		// Colour
		if($search_colour != "all"){
			$condition .= " And `tl_catalog_attribute`.`id_color` = '" . $search_colour . "'";
		}
		
		// Size
		if($search_size != "all"){
			$condition .= " And `tl_catalog_attribute`.`id_size` = '" . $search_size . "'";
		}		
		
		// Price		
		if($search_price != "all"){
			switch($search_price){
				case 1:
					$condition .= " And (price>50 and price<=200) ";
					break;
				case 2:
					$condition .= " And price>200 ";
					break;
				default:
					$condition .= " And price<=50 ";
					break;
			}
		}

		return $condition;
	}
	
	public function getCondition()
	{
		// Get event
		if($_SESSION['perPageRecord'] == ""){
			$_SESSION['perPageRecord'] = 8;
		}else{
			if(isset($_GET['perPageRecord']) && $_GET['perPageRecord'] != "") $_SESSION['perPageRecord'] = $_GET['perPageRecord'];
		}
		
		// product type
		if($this->pageTitle == "Home" && $_SERVER["QUERY_STRING"]==""){
			unset($_SESSION['productType']);
		}

		if(isset($_GET['type']) && $_GET['type'] != ""){
			$_SESSION['productType'] = $_GET['type'];
		}
		else
		{
			$_SESSION['productType'] = "hot";
		}

		switch($_SESSION['productType']){
			case "hot":
				$condition = " where hot=1 and published=1 ";
				break;
			case "new":
				$condition = " where new=1 and price<50 and published=1 ";
				break;
			case "xxxxl":
				$condition = " where xxxxl=1 and published=1 ";
				break;
			case "sales":
				$condition = " where sales=1 and published=1 ";
				break;
			case "all":
				$condition = " where published=1 ";
				break;
			default:			
				$condition = " where hot=1 and published=1 ";
				break;		
		}

		// Category Or Subcategory
		if(isset($_GET['category']) && $_GET['category'] != "") {
			$sql 		= "SELECT pid FROM tl_taxonomy where id=".$_GET['category'];
			$result 	= @mysql_query($sql);
			$countRd 	= @mysql_fetch_assoc($result);
			if($countRd['pid']==0){
				$condition .= " And category IN ( SELECT id FROM `tl_taxonomy` WHERE pid =".$_GET['category'].") ";
			}else{
				$condition .= " And category=".$_GET['category'];
			}
		}

		// Brand
		if(isset($_GET['brand']) && $_GET['brand'] != "") $condition .= " And brand=".$_GET['brand'];
		
		// Tag
		if (isset($_GET['tag']) && $_GET['tag'])
		{
			switch($_SESSION['productType']){
				case "hot":
					$condition = " where `product_catalog`.hot=1 and `product_catalog`.published=1 ";
					break;
				case "new":
					$condition = " where `product_catalog`.new=1 and `product_catalog`.price<50 and `product_catalog`.published=1 ";
					break;
				case "xxxxl":
					$condition = " where `product_catalog`.xxxxl=1 and `product_catalog`.published=1 ";
					break;
				case "sales":
					$condition = " where `product_catalog`.sales=1 and `product_catalog`.published=1 ";
					break;
				case "all":
					$condition = " where `product_catalog`.published=1 ";
					break;
				default:			
					$condition = " where `product_catalog`.hot=1 and `product_catalog`.published=1 ";
					break;		
			}
			
			$condition .= " And `catalog_tags`.`id`=".$_GET['tag'];
		}			

		return $condition;
	}
}

$objRd = new GetPager();
$objRd->run();
?>